using System;
using System.Data;
using Jalal.Library.DataStructure;
using Jalal.Library.Data;
using BCGC.DAL;
using Jalal.Library.Utility;

namespace BCGC.BLL.Forum
{

    public class PostTag
    {

        private int postTagID;
        public int PostTagID
        {
            get
            {
                return postTagID;
            }
        }

        private String tag;
        public String Tag
        {
            get
            {
                return tag;
            }
            set
            {
                tag = value;
            }
        }

        private int tagWeight;
        public int TagWeight
        {
            get
            {
                return tagWeight;
            }
            set
            {
                tagWeight = value;
            }
        }

        public PostTag()
        {
        }

        public PostTag(DataRow dataRow)
        {
            if (dataRow != null)
            {
                postTagID = (int)CommonUtility.FilterNull(dataRow["PostTagID"], typeof(int));
                tagWeight = (int)CommonUtility.FilterNull(dataRow["TagWeight"], typeof(int));
                tag = (string)CommonUtility.FilterNull(dataRow["Tag"], typeof(string));
             }
         }


        private static ReadWriteCustomCollection<PostTag> GetCollection(DataRowCollection rowCollection)
        {
            ReadWriteCustomCollection<PostTag> collection = new ReadWriteCustomCollection<PostTag>();

            foreach (DataRow dr in rowCollection)
            {
                collection.Add(new PostTag(dr));
            }
            return collection;
        }


        public int Create()
        {
            int result = PostTagData.CreatePostTag(this.Tag,this.TagWeight);
            return result;
        }

        public bool Update()
        {
            bool result = PostTagData.UpdatePostTag(this.PostTagID,this.Tag, this.TagWeight);
            return result;
        }

        public bool Save()
        {
             if (this.postTagID>0)
            {
                return Update();
            }
            else 
            {
                int result = Create();
                if (result > 0)
                {
                    this.postTagID = result;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            
        }

        public static bool Delete(int tagID)
        {
            DataFactory.GetDataMiner().BeginTransaction(false);
            bool result = PostTagData.DeletePostTag(tagID);
            DataFactory.GetDataMiner().CommitTransaction();
            return result;
        }

        public static PostTag GetByTagName(String tagName)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = PostTagData.GetByTagName(tagName);
            DataFactory.GetDataMiner().CommitTransaction();
            if (result.Rows.Count > 0)
                return new PostTag(result.Rows[0]);
            else
                return null;
        }

        public static ReadWriteCustomCollection<PostTag> GetAllPostTag()
        {

            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = PostTagData.GetAllPostTag();
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return GetCollection(result.Rows);
            else
                return null;
        }

    }

}
